Skip to content

feat: add n_input_nand_gate function to compute NAND for multiple inputs#14625

Open
TTAAAN wants to merge 4 commits intoTheAlgorithms:masterfrom
TTAAAN:add/n_input_nand_gate
Open

feat: add n_input_nand_gate function to compute NAND for multiple inputs#14625
TTAAAN wants to merge 4 commits intoTheAlgorithms:masterfrom
TTAAAN:add/n_input_nand_gate

Conversation

@TTAAAN
Copy link
Copy Markdown

@TTAAAN TTAAAN commented May 4, 2026

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@TTAAAN TTAAAN marked this pull request as ready for review May 4, 2026 04:42
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels May 4, 2026
@TTAAAN TTAAAN marked this pull request as draft May 4, 2026 04:44
@TTAAAN TTAAAN marked this pull request as ready for review May 4, 2026 04:54
if len(inputs) < 2:
raise ValueError("Input list must contain at least two elements")

return int(not all(inputs))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elegant one-liner — not all(inputs) correctly implements n-input
NAND logic since NAND outputs 0 only when ALL inputs are 1.
Wrapping with int() cleanly converts bool to 0/1.

One edge case worth considering: what if inputs contains values
other than 0 and 1 (e.g. 2 or -1)? all() treats any non-zero
value as True, which could give unexpected results. Consider
adding a validation:

if not all(i in (0, 1) for i in inputs):
raise ValueError("All inputs must be 0 or 1")

return int(not (input_1 and input_2))


def n_input_nand_gate(inputs: list[int]) -> int:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good addition extending the existing nand_gate() to support n
inputs. The function name and type hints are clear and consistent
with the repo's style.

Suggestion: the docstring could mention the relationship to the
existing 2-input nand_gate() for context:

"Generalization of nand_gate() to support n inputs.
Returns 0 only when all inputs are 1, 1 otherwise."

ValueError: Input list must contain at least two elements
"""

if len(inputs) < 2:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good guard for minimum input length. Worth also adding a check
for empty list explicitly since len([]) < 2 is True and would
raise the same error — but the error message "at least two
elements" might confuse users who pass an empty list.

Consider:
if len(inputs) == 0:
raise ValueError("Input list cannot be empty")
if len(inputs) < 2:
raise ValueError("Input list must contain at least two elements")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants